package com.maket.jbxx.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.maket.jbxx.domain.CpAttributeType;
import com.maket.jbxx.domain.CpflProductClassification;
import com.maket.jbxx.domain.SxAttributeValue;
import com.maket.jbxx.service.ICpAttributeTypeService;
import com.maket.jbxx.service.ICpflProductClassificationService;
import com.maket.jbxx.service.ISxAttributeValueService;
import com.ruoyi.system.api.CodeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.maket.jbxx.domain.SxProductAttribute;
import com.maket.jbxx.service.ISxProductAttributeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 属性值Controller
 * 
 * @author ruoyi
 * @date 2024-07-02
 */
@RestController
@RequestMapping("/attribute")
public class SxProductAttributeController extends BaseController
{
    @Autowired
    private ISxProductAttributeService sxProductAttributeService;

    @Resource
    private CodeClient codeClient;

    @Autowired
    private ICpAttributeTypeService cpAttributeTypeService;

    @Autowired
    private ISxAttributeValueService sxAttributeValueService;

    @Autowired
    private ICpflProductClassificationService cpflProductClassificationService;

    /**
     * 查询属性值列表
     */
    @RequiresPermissions("jbxx:attribute:list")
    @GetMapping("/list")
    public TableDataInfo list(SxProductAttribute sxProductAttribute)
    {
        startPage();
        List<SxProductAttribute> list = sxProductAttributeService.selectSxProductAttributeList(sxProductAttribute);

        return getDataTable(list);
    }


    /**
     * 查询分类值列表
     */
    @RequiresPermissions("jbxx:attribute:list")
    @GetMapping("/cpflList")
    public TableDataInfo cpflList(CpflProductClassification  cpflProductClassification)
    {
        startPage();
        List<CpflProductClassification> list = cpflProductClassificationService.selectCpflProductClassificationList(cpflProductClassification);

        return getDataTable(list);
    }

    /**
     * 导出属性值列表
     */
    @RequiresPermissions("jbxx:attribute:export")
    @Log(title = "属性值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SxProductAttribute sxProductAttribute)
    {
        List<SxProductAttribute> list = sxProductAttributeService.selectSxProductAttributeList(sxProductAttribute);
        ExcelUtil<SxProductAttribute> util = new ExcelUtil<SxProductAttribute>(SxProductAttribute.class);
        util.exportExcel(response, list, "属性值数据");
    }

    /**
     * 获取属性值详细信息
     */
    @RequiresPermissions("jbxx:attribute:query")
    @GetMapping(value = "/{sxCode}")
    public AjaxResult getInfo(@PathVariable("sxCode") String sxCode)
    {

        return success(sxProductAttributeService.selectSxProductAttributeBySxCode(sxCode));
    }


    /**
     * 获取产品分类值详细信息
     */
    @RequiresPermissions("jbxx:attribute:query")
    @GetMapping(value = "/cpflListSxCode/{sxCode}")
    public AjaxResult getCpTypeInfo(@PathVariable("sxCode") String sxCode)
    {

        return success(sxProductAttributeService.selectCpAttributeTypeBySxCode(sxCode));
    }

    /**
     * 新增属性值
     */
    @RequiresPermissions("jbxx:attribute:add")
    @Log(title = "属性值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SxProductAttribute sxProductAttribute)
    {
        sxProductAttribute.setSxCode(codeClient.getCode(24));

        sxProductAttributeService.insertSxProductAttribute(sxProductAttribute);

        sxProductAttribute.getSxAttributeValueListCpType().forEach(p -> p.setSxCode(sxProductAttribute.getSxCode()));

        sxProductAttribute.getSxAttributeValueListCpType().forEach(p->p.setProductTypeCode(p.getCpflCode()));

        return toAjax(cpAttributeTypeService.batchInsertCpAttributeType(sxProductAttribute.getSxAttributeValueListCpType()));
    }


    /**
     * 修改属性值
     */
    @RequiresPermissions("jbxx:attribute:edit")
    @Log(title = "属性值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SxProductAttribute sxProductAttribute)
    {
        return toAjax(sxProductAttributeService.updateSxProductAttribute(sxProductAttribute));
    }


    /**
     * 修改属性值
     */
    @RequiresPermissions("jbxx:attribute:edit")
    @Log(title = "属性状态", businessType = BusinessType.UPDATE)
    @PutMapping("isStart")
    public AjaxResult editIsStart(@RequestBody SxProductAttribute sxProductAttribute)
    {
        return toAjax(sxProductAttributeService.updateSxProductAttributeIsStart(sxProductAttribute));
    }


    /**
     * 删除属性值
     */
    @RequiresPermissions("jbxx:attribute:remove")
    @Log(title = "属性值", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sxCodes}")
    public AjaxResult remove(@PathVariable String[] sxCodes)
    {
        return toAjax(sxProductAttributeService.deleteSxProductAttributeBySxCodes(sxCodes));
    }
}
